Imports ValidationFramework.Web.ASP
Imports QSBusinessLayerVB
Imports System


Partial Public Class TraditionalForm
  Inherits Page
  ' Methods
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not MyBase.IsPostBack Then
            PopulatePerson()
    End If
        validatorGenerator.AddAssociation(firstNameTextBox, "FirstName", firstNamePlaceHolder)
        validatorGenerator.AddAssociation(lastNameTextBox, "LastName", lastNamePlaceHolder)
        validatorGenerator.AddAssociation(ageTextBox, "Age", agePlaceHolder)
        validatorGenerator.AddAssociation(emailAddressTextBox, "EmailAddress", emailPlaceHolder)
        validatorGenerator.AddAssociation(creditCardNumberTextBox, "CreditCardNumber", cardNumberPlaceHolder)
        validatorGenerator.AddAssociation(orderTotalTextBox, "OrderTotal", orderTotalPlaceHolder)
        validatorGenerator.AddAssociation(phoneNumberTextBox, "PhoneNumber", phoneNumberPlaceholder)
        validatorGenerator.GenerateValidators()
  End Sub

  Private Sub PopulatePerson()
    Dim person As Person = New PersonMapper().GetItem(1)
        firstNameTextBox.Text = person.FirstName
        lastNameTextBox.Text = person.LastName
        ageTextBox.Text = person.Age.ToString
        phoneNumberTextBox.Text = person.PhoneNumber.ToString
        emailAddressTextBox.Text = person.EmailAddress
        cardTypeDropDownList.SelectedValue = person.CardType.ToString
        creditCardNumberTextBox.Text = person.CreditCardNumber
        orderTotalTextBox.Text = person.OrderTotal.ToString
  End Sub

  Protected Sub updateLinkButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    If MyBase.IsValid Then
      Dim personMapper As PersonMapper = New PersonMapper()
            personMapper.SaveOrUpdate(editPerson)
    End If
  End Sub

  Public Overrides Sub Validate()
    Dim age As Integer
    Dim orderTotal As Decimal
        editPerson = New Person()
        editPerson.FirstName = firstNameTextBox.Text
        editPerson.LastName = lastNameTextBox.Text
        editPerson.EmailAddress = emailAddressTextBox.Text
        editPerson.PhoneNumber = phoneNumberTextBox.Text
        editPerson.CreditCardNumber = creditCardNumberTextBox.Text
        editPerson.CardType = New Nullable(Of CardType)(DirectCast([Enum].Parse(GetType(CardType), cardTypeDropDownList.SelectedValue), CardType))
        If Integer.TryParse(ageTextBox.Text, age) Then
            editPerson.Age = age
        End If
        If Decimal.TryParse(orderTotalTextBox.Text, orderTotal) Then
            editPerson.OrderTotal = age
        End If
    MyBase.Validate()
  End Sub

  Protected Sub validatorGenerator_TargetObjectRequired(ByVal sender As Object, ByVal e As TargetObjectRequiredEventArgs)
        e.TargetObject = editPerson
  End Sub


  ' Fields
  Private editPerson As Person
End Class


